<?php
session_start();

class Identity {

    public function isAuth() {
        if (isset($_SESSION["is_auth"])) {
            return $_SESSION["is_auth"];
        }
        else
            return false;
    }

    public function login($email, $password) {
        require_once __DIR__ . '/../models/UserModel.php';
        $user = new UserModel();
        $userIdentity = $user->getUserByEmail($email);
        if (!empty($userIdentity)){
            if ($email == $userIdentity->email && md5($password) == $userIdentity->password_hash) {
                $_SESSION["is_auth"] = true;
                $_SESSION["User"] = $userIdentity;
                return true;
            }
            else {
                $_SESSION["is_auth"] = false;
                return false;
            }
        }else
            return false;
    }

    public function getUser() {
        if ($this->isAuth()) {
            return $_SESSION["User"];
        }
    }


    public function logout() {
        $_SESSION = array();
        session_destroy();
    }
}